local sk__longbian = fk.CreateSkill {

  name = "sk__longbian",

  tags = {  },

}



sk__longbian:addEffect(fk.TurnStart, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(sk__longbian.name) then
      local choices, nums = {}, {}
      local card = Fk:cloneCard("slash")
      local n = card.skill:getMaxUseTime(player, Player.HistoryPhase, card, nil) or 0
      if n > 0 then
        table.insert(choices, "sk__duzun1")
      end
      table.insert(nums, n)
      if player:getMark(MarkEnum.SkDrawNCards) + 2 > 0 then
        table.insert(choices, "sk__duzun2")
      end
      table.insert(nums, math.max(player:getMark(MarkEnum.SkDrawNCards) + 2, 0))
      table.insert(choices, "sk__duzun3")
      table.insert(nums, player.maxHp)
      if #choices > 1 then
        event:setCostData(self, {choices, nums})
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local choices=player.room:askToChoices(player,{
      choices=event:getCostData(self)[1],
      min_num=2,
      max_num=2,
      skill_name=sk__longbian.name,
      prompt="#sk__longbian-invoke:::"..table.concat(event:getCostData(self)[2], ","),
      cancelable=true,
      detailed=false,
      all_choices={"sk__duzun1", "sk__duzun2", "sk__duzun3"}
    })
    if #choices == 2 then
      event:setCostData(self, {choices, event:getCostData(self)[2]})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local nums = event:getCostData(self)[2]
    if table.contains(event:getCostData(self)[1], "sk__duzun1") then
      if table.contains(event:getCostData(self)[1], "sk__duzun2") then
        local n = nums[1] - nums[2]
        if n ~= 0 then
          room:setPlayerMark(player, MarkEnum.SlashResidue, player:getMark(MarkEnum.SlashResidue) - n)
          room:setPlayerMark(player, MarkEnum.SkDrawNCards, player:getMark(MarkEnum.SkDrawNCards) + n)
        end
        room:changeMaxHp(player, 1)
      else
        local n = nums[1] - player.maxHp
        if n ~= 0 then
          room:setPlayerMark(player, MarkEnum.SlashResidue, player:getMark(MarkEnum.SlashResidue) - n)
          room:changeMaxHp(player, n)
        end
        if not player.dead then
          room:addPlayerMark(player, MarkEnum.SkDrawNCards, 1)
        end
      end
    else
      local n = nums[2] - player.maxHp
      if n ~= 0 then
        room:setPlayerMark(player, MarkEnum.SkDrawNCards, player:getMark(MarkEnum.SkDrawNCards) - n)
        room:changeMaxHp(player, n)
      end
      if not player.dead then
        room:addPlayerMark(player, MarkEnum.SlashResidue, 1)
      end
    end
  end,
})

return sk__longbian